package com.caiyouxi.finance.service;

import com.caiyouxi.common.Constant;
import com.caiyouxi.common.service.BaseService;
import com.caiyouxi.finance.mapper.FinanceAccountDetailMapper;
import com.caiyouxi.finance.model.FinanceAccountDetail;
import com.caiyouxi.util.NumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.Date;


/**
 * 用户账户流水记录
 * chensong
 */
@Service
public class FinanceAccountDetailService extends BaseService {
    
    @Autowired
    private FinanceAccountDetailMapper financeAccountDetailMapper;

    /**
     * 新建账户明细流水
     * @param finance_account_id
     * @param userId
     * @param account_code
     * @param balance
     * @param totalAmount
     * @param receivableId
     * @param detailsId
     */
    @Transactional
    public FinanceAccountDetail add_finance_account_details(BigInteger finance_account_id,Integer userId,String account_code,Integer balance,Integer totalAmount,BigInteger receivableId,BigInteger detailsId){
        FinanceAccountDetail fad = new FinanceAccountDetail();
        fad.setId(new BigInteger(NumUtil.generate_number(4)));
        fad.setFinance_account_id(finance_account_id);
        fad.setUser_id(userId);
        fad.setAccount_code(account_code);
        fad.setBeginning_balance(balance);
//        Log.d("充值前余额" + balance);
        fad.setDebit_or_credit("DEBIT");
        fad.setFund_type("PURCHASE");
        fad.setAmount(totalAmount);
//        Log.d("充值金额" + totalAmount);
        fad.setFinance_trade_receivable_id(receivableId);
        fad.setFinance_trade_details_id(detailsId);
//        Log.d("充值后余额" + (balance + totalAmount));
        fad.setBalance(balance + totalAmount);
        fad.setStatus(Constant.USER_STATUS_UNCONFIRMED);//创建后待确认的。其实也是被冻结状态。
        fad.setCreate_time(new Date());
        fad.setExpire_time(null);
        fad.setUpdate_time(new Date());
        financeAccountDetailMapper.add_finance_account_details(fad);
        return fad;
    }
    
    public void update_finance_account_details(FinanceAccountDetail fad){
        financeAccountDetailMapper.update_finance_account_details(fad);
    }
}
